package webserver.bank;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.Writer;
import java.net.Socket;
import java.net.UnknownHostException;

public class ClientStub
{
	private static final String CLRF = "\r\n";
	String httpversion = "HTTP/1.0";
	int objectNr;
	Socket socket;

	public ClientStub(int objectref)
	{
		objectNr = objectref;
	}

	public ClientStub(String className, Class<?>[] parameterTypes,
			Object[] parameterValues) throws UnknownHostException, IOException
	{
		socket = new Socket(ClientStubSetting.getHost(), ClientStubSetting
				.getClientPort());
		Writer out = new PrintWriter(socket.getOutputStream());

		BufferedReader in = new BufferedReader(new InputStreamReader(socket
				.getInputStream()));

		if (parameterTypes.length == 0 & parameterValues.length == 0)
		{
			out.write("POST /" + ClientStubSetting.getProtocolName() + "/"
					+ className + "/ " + httpversion);
		}

		else if (parameterTypes.length > 0 & parameterValues.length > 0
				&& parameterTypes.length == parameterValues.length)
		{
			out.write("POST /" + ClientStubSetting.getProtocolName() + "/"
					+ className + "/");
			for (int i = 0; i < parameterValues.length; i++)
			{
				out.write(parameterTypes[i].getName() + "="
						+ parameterValues[i] + "&");
			}
			out.write(" " + httpversion);
		}
		out.flush();
		socket.shutdownOutput();
		String lastLine = null;
		String lines = "";

		while ((lastLine = in.readLine()) != null)
		{
			lines += lastLine + " ";
		}

		if (lines.startsWith("200 All ok Content-Type: object "))
		{
			objectNr = Integer.parseInt(lines.substring(32, lines
					.lastIndexOf(" ")));
		}
		else
		{
			System.out.println("Returned string mismatch");
		}
		socket.close();
	}

	public Object performMethodCall(String className, String methodName,
			Class<?>[] parameterTypes, Object[] parameterValues)
	{
		try
		{
			socket = new Socket(ClientStubSetting.getHost(), ClientStubSetting
					.getClientPort());
			Writer out = new PrintWriter(socket.getOutputStream(), true);
			out.write("GET /" + ClientStubSetting.getProtocolName() + "/"
					+ className + "/" + objectNr + "/" + methodName + "/");

			for (int i = 0; i < parameterTypes.length;)
			{
				out.write(parameterTypes[i].getName() + "=");
				out.write(parameterValues[i] + "");
				out.write("&");
				i++;
			}
			out.write(" " + httpversion);
			out.flush();
			socket.shutdownOutput();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}

		try
		{
			BufferedReader in = new BufferedReader(new InputStreamReader(socket
					.getInputStream()));

			String lastLine;
			String lines = "";

			while ((lastLine = in.readLine()) != null)
			{
				lines += lastLine + " ";
			}
			if (lines.startsWith("200 All ok Content-Type: object"))
			{
				return lines.substring(32, lines.lastIndexOf(" "));
			}
			else
			{
				System.out.println(lines);
				System.out.println(lines.substring(32));

				System.out.println("Returned string mismatch_");
			}
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		return null;
	}
}
